package com.qfedu.fmmall;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qfedu.fmmall.dao.ProductMapper;
import com.qfedu.fmmall.vo.Product4ES;
import com.qfedu.fmmall.vo.ProductVO;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.List;

/**
 * @author : yanBin
 * @date :2022/1/14 11:29
 * 把product表中数据化到es
 */
@SpringBootTest(classes = ApiApplication.class)
@RunWith(SpringRunner.class)
public class ImportProductInfoIntoES {


    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ObjectMapper objectMapper;


    /**
     * 创建es索引
     */
    @Test
    public void addCreateIndex() throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("fmmallproducrsindex");
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.isAcknowledged());

    }

    @Test
    public void addProductCount() throws IOException {
        //查询数据库商品所有数据
        List<ProductVO> productVOS = productMapper.selectProductCount();
        //将查询的数据写入到es
        for (ProductVO productVO : productVOS) {
            Product4ES product4ES = new Product4ES();
            product4ES.setProductId(productVO.getProductId());
            product4ES.setProductName(productVO.getProductName());
            if (productVO.getImgs() != null && productVO.getImgs().size() > 0 && productVO.getImgs().get(0) != null) {
                product4ES.setImgId(productVO.getImgs().get(0).getId());
                product4ES.setProductImg(productVO.getImgs().get(0).getUrl());
            }
            if (productVO.getSkus() != null && productVO.getSkus().size() > 0 && productVO.getSkus().get(0) != null) {
                product4ES.setProductSkuName(productVO.getSkus().get(0).getSkuName());
                product4ES.setSkuId(productVO.getSkus().get(0).getSkuId());
                product4ES.setProductSkuPrice(productVO.getSkus().get(0).getSellPrice());
            }
            //表示要在index3索引里面添加数据
            IndexRequest indexRequest = new IndexRequest("fmmallproducrsindex");
            indexRequest.id(product4ES.getProductId());
            indexRequest.source(objectMapper.writeValueAsBytes(product4ES), XContentType.JSON);
            //执行es访问获取结果
            IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            System.out.println(indexResponse.isFragment());
        }
    }


}
